package com.chixing.day16.Reflect;


import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

/**
 * A.java -- A.class  --在JVM中 Class
 * 通过字节码文件得到该类的结构
 * Student:成员变量     构造函数       方法        父类           接口          注释
 * (字段Field) (Constructor) (Method) （superClass）  (interface)  (annotation)
 * <p>
 * <p>
 * Method:修饰符 + 返回值类型 + 方法名 + 参数列表 + 异常
 * 修饰符（public abstract static final native....）
 */
public class ReflectDemo {
    public static void main(String[] args) {

        Class<Student> clas = Student.class;
        clas.getMethods();

        Student student = null;
        try {
            student = clas.newInstance(); //依赖于无参构造函数
            System.out.println("实例化student对象");
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        //获得所有的方法 Method
        Method[] methods = clas.getDeclaredMethods();
        for (Method method : methods) {

            // Modifier dir;
            String me = method.getName();
            int modifiers = method.getModifiers();
            Class<?> returnType = method.getReturnType();
            Class<?>[] parameterTypes = method.getParameterTypes();
            Class<?>[] exceptionTypes = method.getExceptionTypes();
            Class<Student> claz = Student.class;

            System.out.print("类名: " + claz);
            System.out.print("方法名称: " + me + "  ");
            System.out.print("访问修饰符: " + modifiers + "  ");
            System.out.print("返回值类型: " + returnType + "  ");

            for (Class<?> parameterType : parameterTypes) {
                System.out.print("参数列表: " + parameterType);
            }
            System.out.println();
        }

        System.out.println("========================================");

        //方法调用 Object result = stu1.play(int,float)
        //获取play方法的返回值
        Method method = null;
        try {
            method = clas.getMethod("play", int.class, float.class);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }

        //resul： retuen value
        Object result = null;
        try {
            result = method.invoke(student, 10, 9.9f);
            System.out.println("play方法的返回值: " + result);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        System.out.println("===================================");

        //获取 static study方法的反回值

        try {
            method = clas.getMethod("study", int.class, String.class, Float.class);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }

        try {
            result = method.invoke(Student.class, 10, ",", 9.9f);
            System.out.println("study方法的反回值：" + result);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        System.out.println("===================================");
        //获取pay方法的返回值
        Method method1 = null;
        try {
            method1 = clas.getMethod("pay", int.class, float.class);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }

        Object result1 = null;
        try {
            result1 = method1.invoke(student, 10, 9.9f);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        System.out.println("pay方法的返回值： " + result1);

        System.out.println("===================================");




        //获得所有的字段 Field
        Field[] fields = clas.getDeclaredFields();
        for (Field field : fields) {

            String name = field.getName();
            Class<?> gt = field.getType();


            System.out.print("字段名: " + name + "  ");
            System.out.println("类型：" + gt + " ");
        }


    }
}
